package cn.iocoder.yudao.module.infrastructure.user.controller.admin;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.infrastructure.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.infrastructure.user.dal.dataobject.AdminUserDO;
import cn.iocoder.yudao.module.infrastructure.user.dal.mysql.AdminUserMapper;
import cn.iocoder.yudao.module.infrastructure.user.vo.AdminUserPickReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2024/12/29
 * @see AdminUserController
 */
@Tag(name = "管理后台 - 后台用户")
@RestController
@RequestMapping("/system/user")
@Validated
public class AdminUserV2Controller {
    @Resource
    private AdminUserMapper adminUserMapper;

    @GetMapping("/pick")
    @Operation(summary = "获得后台用户分页（用于选择用户）")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin','shop_admin')")
    public CommonResult<List<AdminUserRespDTO>> getUserPageForPick(@Valid AdminUserPickReqVO reqVO) {
        if (StrUtil.isBlank(reqVO.getMobile()) && StrUtil.isBlank(reqVO.getMobile())) {
            return CommonResult.success(Collections.emptyList());
        }
        List<AdminUserDO> list = adminUserMapper.selectList(new CoreLambdaQueryWrapper<AdminUserDO>()
                .likeIfPresent(AdminUserDO::getMobile, reqVO.getMobile())
                .likeIfPresent(AdminUserDO::getNickname, reqVO.getNickname()));
        return CommonResult.success(BeanUtils.toBean(list, AdminUserRespDTO.class));
    }

}
